<?php

namespace app\models;

use Yii;

/**
 * This is the model class for table "user".
 *
 * @property integer $id
 * @property string $username
 * @property string $email
 * @property string $password_hash
 * @property string $access_token
 * @property string $auth_key
 * @property integer $confirmed_at
 * @property string $unconfirmed_email
 * @property integer $blocked_at
 * @property string $registration_ip
 * @property integer $created_at
 * @property integer $updated_at
 * @property integer $flags
 * @property integer $role_id
 *
 * @property Event[] $events
 * @property Member[] $members
 * @property Mission[] $missions
 * @property Project[] $projects
 * @property Profile $profile
 * @property SocialAccount[] $socialAccounts
 * @property Token[] $tokens
 * @property Role $role
 */
class User extends \dektrium\user\models\User  //\yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'user';
    }

    /**
     * 添加场景
     * @return string
     */
    public function scenarios()
    {
        $scenarios = parent::scenarios();
        //CRUD需要部门id
        $scenarios['create'][]   = 'departmentid';
        $scenarios['update'][]   = 'departmentid';
        $scenarios['register'][] = 'departmentid';
//        //需要dinguserid
//        $scenarios['create'][]   = 'dinguserid';
//        $scenarios['update'][]   = 'dinguserid';
//        $scenarios['register'][] = 'dinguserid';
        return $scenarios;
    }
    
    /**
     * @inheritdoc
     */
    public function rules()
    {
//        return [
//            [['username', 'email', 'password_hash', 'auth_key', 'created_at', 'updated_at'], 'required'],
//            [['confirmed_at', 'blocked_at', 'created_at', 'updated_at', 'flags', 'role_id'], 'integer'],
//            [['username'], 'string', 'max' => 25],
//            [['email', 'access_token', 'unconfirmed_email'], 'string', 'max' => 255],
//            [['password_hash'], 'string', 'max' => 60],
//            [['auth_key'], 'string', 'max' => 32],
//            [['registration_ip'], 'string', 'max' => 45],
//            [['username'], 'unique'],
//            [['email'], 'unique']
//        ];
        $rules = parent::rules();
        //部门id必须
//        $rules['fieldRequired'] = ['departmentid', 'required'];
//        
//        
//        $rules['fieldLength']   = ['departmentid', 'integer'];

        return $rules;
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'username' => 'Username',
            'email' => 'Email',
            'password_hash' => 'Password Hash',
            'access_token' => 'Access Token',
            'auth_key' => 'Auth Key',
            'confirmed_at' => 'Confirmed At',
            'unconfirmed_email' => 'Unconfirmed Email',
            'blocked_at' => 'Blocked At',
            'registration_ip' => 'Registration Ip',
            'created_at' => 'Created At',
            'updated_at' => 'Updated At',
            'flags' => 'Flags',
            'role_id' => 'Role ID',
            'departmentid'=>'Departmentid',
            'dinguserid'=>'Dinguserid'
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getEvents()
    {
        return $this->hasMany(Event::className(), ['creater_id' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getMembers()
    {
        return $this->hasMany(Member::className(), ['user_id' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getMissions()
    {
        return $this->hasMany(Mission::className(), ['charge_id' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getProjects()
    {
        return $this->hasMany(Project::className(), ['manager_id' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getProfile()
    {
        return $this->hasOne(\dektrium\user\models\Profile::className(), ['user_id' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getSocialAccounts()
    {
        return $this->hasMany(SocialAccount::className(), ['user_id' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getTokens()
    {
        return $this->hasMany(Token::className(), ['user_id' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getRole()
    {
        return $this->hasOne(Role::className(), ['id' => 'role_id']);
    }

    public function getAnswers(){

        return $this->hasMany(Token::className(), ['sender_id' => 'id']);
    }

    /**
     * @inheritdoc
     */
    public static function findIdentity($id) {
        return static::findOne($id);
        //return isset(self::$users[$id]) ? new static(self::$users[$id]) : null;
    }

    /**
     * @inheritdoc
     */
    public static function findIdentityByAccessToken($token, $type = null) {
        return static::findOne(['access_token' => $token]);
        /* foreach (self::$users as $user) {
          if ($user['accessToken'] === $token) {
          return new static($user);
          }
          }

          return null; */
    }

    /**
     * Finds user by username
     *
     * @param  string      $username
     * @return static|null
     */
    public static function findByUsername($username) {
        $user = User::find()
            ->where(['username' => $username])
            ->asArray()
            ->one();

        if ($user) {
            return new static($user);
        }

        return null;
        /* foreach (self::$users as $user) {
          if (strcasecmp($user['username'], $username) === 0) {
          return new static($user);
          }
          }

          return null; */
    }

    /**
     * @inheritdoc
     */
    public function getId() {
        return $this->id;
    }
    public static function getUsernameById($id){
        $user=  User::find()
                ->where(['id'=>$id])
                ->one();
        return $user->username;
    }
    public static function getUserEmailById($id){
        $user=  User::find()
                ->where(['id'=>$id])
                ->one();
        return $user->email;
    }
    /**
     * @inheritdoc
     */
    public function getAuthKey() {
        return $this->auth_key;
    }

    /**
     * @inheritdoc
     */
    public function validateAuthKey($authKey) {
        return $this->auth_key === $authKey;
    }

    /**
     * Validates password
     *
     * @param  string  $password password to validate
     * @return boolean if password provided is valid for current user
     */
    public function validatePassword($password) {
        return $this->password === $password;
    }
}
